<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'
import { eliminateReasonInfo } from '@/api/charts'
export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      chart: null,
      reasonInfo: [
        { experienceCount: '工作经验不匹配' }, //
        { technologyCount: '技术不匹配' }, //
        { salaryCount: '薪酬不匹配' }, //
        { educationCount: '学历不匹配' }, //
        { industryCount: '行业不匹配' }, //
        { typeCount: '招聘类型不匹配' } //
      ]
    }
  },
  mounted() {
    eliminateReasonInfo().then(response => {
      const mixData = []
      this.reasonInfo.forEach(item => {
        mixData.push([response.result[Object.keys(item)[0]], item[Object.keys(item)[0]]])
      })
      this.initChart(mixData)
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart(mixData) {
      this.chart = echarts.init(this.$el, 'macarons')

      this.chart.setOption({
        dataset: {
          source: mixData
        },
        xAxis: { name: '单位/人', nameLocation: 'end', nameRotate: 270, nameGap: 10, minInterval: 1 },
        yAxis: { type: 'category' },
        grid: {
          top: 10,
          left: '2%',
          right: '5%',
          bottom: '3%',
          containLabel: true
        },
        series: [
          {
            type: 'bar',
            barCategoryGap: 10,
            barWidth: '60%',
            encode: {
              // Map the "amount" column to X axis.
              x: 'amount',
              // Map the "product" column to Y axis
              y: 'product'
            }
          }
        ]
      })
    }
  }
}
</script>
